package com.miyake.client.datamanage;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.client.DataService;
import com.miyake.client.DataServiceAsync;

public abstract class Page {
	/*
	private List<NodeUpdateHandler> handlers = new ArrayList<NodeUpdateHandler>();
	public void addPageHandler(NodeUpdateHandler handler) {
		handlers.add(handler);
	}
	*/
	public void addNodeUpdateHandler(NodeUpdateHandler updateHandler) {
		this.updateHandler = updateHandler;
	}
	protected void fireNodeUpdateEvent(String key) {
//		for (NodeUpdateHandler handler : handlers) {
//			handler.onNodeUpdate(key);
//		}
		updateHandler.onNodeUpdate(key);
	}
	protected void fireNodeChangeEvent(String key) {
		updateHandler.onNodeChange(key);
	}
	
	protected void fireNodeAddedEvent(String key) {
		updateHandler.onNodeAdd(key);
	}
	
	public void addCreateHandler(ToolBarHandler createHandler) {
		this.createHandler = createHandler;
	}
	
	public void addUpdateHandler(NodeUpdateHandler updateHandler) {
		this.updateHandler = updateHandler;
	}
	
	public void addGuestHandler(GuestHandler guestHandler) {
		this.guestHandler = guestHandler;
	}
	
	private DataServiceAsync service = DataService.Util.getInstance();
	private VerticalPanel panel;
	private NodeProperty node;
	protected ToolBarHandler createHandler = null; // This should be in List? Think it out later.
	protected NodeUpdateHandler updateHandler = null; // Later
	protected GuestHandler guestHandler = null; // Later
	abstract protected Widget getContent(NodeProperty node, DataServiceAsync service);
	
	public Page(NodeProperty node, VerticalPanel panel){
		this.panel = panel;
		this.node = node;
	}
	
	public void update() {
		panel.clear();
		panel.add(new HTML("<h1>"+ node.getName() + "</h1>"));
		panel.add(getContent(node, service));
	}

	
	public NodeProperty getNode() {
		return node;
	}
	public void setNode(NodeProperty node) {
		this.node = node;
	}
	
	public DataServiceAsync getService() {
		return service;
	}
	
	public VerticalPanel getPanel() {
		return panel;
	}
	
	protected boolean canCreate() {
		if (this.createHandler != null) {
			return true;
		}
		return false;
	}

	protected boolean canEdit() {
		if (this.updateHandler != null) {
			return true;
		}
		return false;
	}
	
	protected boolean canGuestOperate() {
		if (this.guestHandler != null) {
			return true;
		}
		return false;
	}
	
	public void setSelected(NodeProperty node) {}

}
